/*
 * Main.cpp
 *
 *  Created on: 31.10.2012
 *      Author: schneider
 */

#include <stdlib.h>
#include <stdio.h>
#include <cstdlib>
#include "CsvLoader.h"
#include "GenCalc.h"
#include "Main.h"
#include "GraphExporter.h"
#include "tbb/tick_count.h"
#include "FastDataApproxImpl.h"

using namespace std;

Main::Main()
{
	loader = new CsvLoader();
	conf = new Configuration();
	calc = NULL;
}

Main::~Main()
{
	delete loader;
	delete calc;
	delete conf;
}

int Main::main(int argc, char* argv[])
{
	if(argc <= 1)
	{
		conf->printUsage();
		exit(EXIT_FAILURE);
	}

	conf->parseArgs(argc, argv);
	int result = loader->loadFile(conf->inputFile);


	if(result == EXIT_SUCCESS)
	{
		conf->printUsedParams();
		calc = new GenCalc(loader->getData(), conf);

		long start = time(NULL);
		calc->initPopulation();		// prvni populace
		calc->nextEvolution(0);		// ohodnoceni prvni pooulace


		calc->compute();			// dopocitej zbytek generaci

		double bestFitness = calc->getBestFitness();
		double* bestMember = calc->getBestMember();

		printf("Final values (fitt=%f):  p=%f, cg=%f, c=%f , dt=%f, k=%f \n", bestFitness,
				bestMember[0],
				bestMember[1],
				bestMember[2],
				bestMember[3],
				bestMember[4]);

		cout << "Done in :" << (time(NULL) - start) << " sec." << endl;

		GraphExporter graphExp(bestMember, loader->getData(), conf->inputFile);
		graphExp.exportSVG();

		return EXIT_SUCCESS;
	}

	return EXIT_FAILURE;
}

int main(int argc, char* argv[])
{
	Main main;
	main.main(argc, argv);
}

